﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;

namespace MonsterMashRegistry
{
    public partial class AddNewLink : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void m_SubmitButton_Click(object sender, EventArgs e)
        {
            String linkName = m_nameLinkBox.Text;
            if (String.IsNullOrEmpty(linkName))
            {
                return;
            }

            String linkUrl = m_urlToLinkBox.Text;
            if (String.IsNullOrEmpty(linkUrl))
            {
                return;
            }

            String name1 = m_paramName1.Text;
            String value1 = m_paramValue1.Text;
            String name2 = m_paramName2.Text;
            String value2 = m_paramValue2.Text;
            String name3 = m_paramName3.Text;
            String value3 = m_paramValue3.Text;

            using (xDB_SOAP.XhiveServerSessionIfClient client = new MonsterMashRegistry.xDB_SOAP.XhiveServerSessionIfClient())
            {
                try
                {
                    String username = Session["Username"].ToString();
                    String password = Session["Password"].ToString();
                    String sessionID = client.connect(username, password, "DL_Monster_Mash_XML");
                    String strXml = null; //client.getDocument(sessionID, username + "/" + "links.xml", null, null);

                    try
                    {
                        strXml = client.getDocument(sessionID, username + "/links.xml", null, null);
                    }
                    catch (Exception ex1)
                    {
                        String errMsg = ex1.ToString();
                    }

                    XmlDocument doc = new XmlDocument();
                    XmlElement topNode = null;
                    XmlElement rootNode = null;

                    if (!String.IsNullOrEmpty(strXml))
                    {
                        doc.LoadXml(strXml);
                        XmlNodeList nodes = doc.GetElementsByTagName("Links");
                        rootNode = doc.DocumentElement;
                        if (null == nodes || nodes.Count == 0)
                        {
                            topNode = doc.CreateElement("Links");
                        }
                        else
                        {
                            topNode = (XmlElement)nodes[0];
                            doc.RemoveChild(topNode);
                        }
                        client.checkOutDocument(sessionID, username + "/links.xml", null);
                    }
                    else
                    {
                        rootNode = doc.CreateElement("MonsterMash2010");
                        topNode = doc.CreateElement("Links");

                    }

                    XmlElement linkToAdd = doc.CreateElement(linkName);
                    XmlElement url = doc.CreateElement("URL");
                    url.InnerText = linkUrl;
                    XmlElement xmlName1 = doc.CreateElement(name1);
                    xmlName1.InnerText = value1;
                    XmlElement xmlName2 = doc.CreateElement(name2);
                    xmlName2.InnerText = value2;
                    XmlElement xmlName3 = doc.CreateElement(name3);
                    xmlName3.InnerText = value3;

                    linkToAdd.AppendChild(url);
                    linkToAdd.AppendChild(xmlName1);
                    linkToAdd.AppendChild(xmlName2);
                    linkToAdd.AppendChild(xmlName3);
                    topNode.AppendChild(linkToAdd);

                    if (!String.IsNullOrEmpty(strXml))
                    {
                        doc.DocumentElement.AppendChild(topNode);
                    }
                    else
                    {
                        rootNode.AppendChild(topNode);
                        doc.AppendChild(rootNode);
                    }

                    if (!String.IsNullOrEmpty(strXml))
                    {
                        client.checkInDocument(sessionID, username + "/links.xml", null, String.Empty, null, doc.OuterXml);
                    }
                    else
                    {
                        try
                        {
                            client.storeDocument(sessionID, username + "/links.xml", false, null, doc.OuterXml);
                        }
                        catch (Exception ex2)
                        {
                            String errMsg = ex2.ToString();
                        }
                    }


                }
                catch (Exception ex)
                {
                    String errMsg = ex.ToString();
                }


            }

            
        }
    }
}
